<?php
namespace Home\Controller;
use Think\Controller;


class Mycollect2Controller extends Controller {
   //收藏货品
   function collectgoods(){
    $collect=M("Mycollect2");
    $needlistid=$_POST["needlistid"];
    $userid=$_POST["userid"];
    $result=$collect->where("needlist_id='$needlistid' and user_id='$userid'")->select();
     if ($result) {
      	  //已经加入，
          $this->ajaxReturn(array(
                  "code"=>500,
                  "message"=>"已经收藏过了！"
                ),"JSON");    
      }
      else{
      	//，需要加入
      	    $newdata["user_id"]=$userid;
        	$newdata["needlist_id"]=$needlistid;
         
            $result=$collect->add($newdata);

            if ($result) {
          	# 成功加入..
          	$this->ajaxReturn(array(
                "code"=>200,
                "message"=>"成功收藏！" 
          		),"JSON");

            }else{
          		$this->ajaxReturn(array(
                "code"=>500,
                "message"=>"加入失败！"
          		),"JSON");
            }
      }

   }

    //移除货品
   function removecollectgoods(){
    $collect=M("Mycollect2");
    $needlistid=$_POST["needlistid"];
    $userid=$_POST["userid"];
    $result=$collect->where("needlist_id='$needlistid' and user_id='$userid'")->select();
     if ($result) {
      	   //，需要加入
      	    $newdata["user_id"]=$userid;
        	  $newdata["needlist_id"]=$supplylistid;
            $result=$collect->where("needlist_id='$needlistid' and user_id='$userid'")->delete();
            if ($result) {
          	# 成功加入..
          	$this->ajaxReturn(array(
                "code"=>200,
                "message"=>"成功删除！" 
          		),"JSON");

            }else{
          		$this->ajaxReturn(array(
                "code"=>500,
                "message"=>"失败！"
          		),"JSON");
            } 
      }

   }

     //查询我收藏的货品
   function Mycollectgoods(){
    $collect=M("Mycollect2");
    $userid=$_POST["userid"];
    $result=$collect
    ->field("address_name,goodsclassify_name,t_user.user_id,t_needlist.needlist_id,t_needlist.needlist_goodsname,needlist_timeinterval,needlist_surplustime,t_needlist.needlist_number,t_needlist.needlist_time,t_needlist.needlist_desc")
    ->join("t_needlist on t_mycollect2.needlist_id=t_needlist.needlist_id")
    ->join("t_user on t_needlist.user_id = t_user.user_id")
    ->join("t_address on t_needlist.address_id=t_address.address_id")
    ->join("t_goodsclassify on t_needlist.goodsclassify_id=t_goodsclassify.goodsclassify_id")
    ->where("t_mycollect2.user_id='$userid'")->select();
     if ($result) {
        # 成功加入..
        $this->ajaxReturn(array(
            "code"=>200,
            "message"=>"成功！" ,
            "result"=>$result
          ),"JSON");

        }else{
          $this->ajaxReturn(array(
            "code"=>500,
            "message"=>"失败！"
          ),"JSON");
        } 
   }






}